<template>
  <div class="cart-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <div class="header-title">
        <i class="el-icon-shopping-cart-1"></i>
        购物车
      </div>
      <div class="header-steps">
        <div class="step-item" :class="{ active: currentStep === 1 }">
          <span class="step-number">1</span>
          <span class="step-text">购物车</span>
        </div>
        <div class="step-line" :class="{ active: currentStep >= 2 }"></div>
        <div class="step-item" :class="{ active: currentStep === 2 }">
          <span class="step-number">2</span>
          <span class="step-text">确认订单</span>
        </div>
        <div class="step-line" :class="{ active: currentStep >= 3 }"></div>
        <div class="step-item" :class="{ active: currentStep === 3 }">
          <span class="step-number">3</span>
          <span class="step-text">支付完成</span>
        </div>
      </div>
    </div>

    <!-- 步骤1: 购物车 -->
    <div v-if="currentStep === 1" class="step-content">
      <!-- 购物车为空状态 -->
      <div v-if="tableData.length === 0" class="empty-cart">
        <div class="empty-icon">
          <i class="el-icon-shopping-cart-1"></i>
        </div>
        <div class="empty-text">购物车还是空的</div>
        <div class="empty-desc">去逛逛，把想买的带回家</div>
        <el-button type="primary" @click="$router.push('/')">
          去购物
        </el-button>
      </div>

      <!-- 购物车有商品 -->
      <div v-else class="cart-with-items">
        <!-- 商品列表头部 -->
        <div class="cart-header">
          <div class="header-product">商品信息</div>
          <div class="header-price">单价</div>
          <div class="header-quantity">数量</div>
          <div class="header-total">小计</div>
          <div class="header-action">操作</div>
        </div>

        <!-- 商品列表 -->
        <div class="cart-items">
          <div v-for="(item, index) in tableData" :key="index" class="cart-item">
            <div class="item-product">
              <div class="product-image">
                <el-image 
                  :src="item.item.image" 
                  fit="cover"
                  class="goods-image"
                >
                  <template #error>
                    <div class="image-error">
                      <i class="el-icon-picture-outline"></i>
                    </div>
                  </template>
                </el-image>
              </div>
              <div class="product-info">
                <div class="product-name">{{ item.item.name }}</div>
                <div class="product-desc">{{ item.item.description }}</div>
                <div class="product-meta">
                  <span class="product-brand">{{ item.item.brand }}</span>
                  <span class="product-category">{{ item.item.category.name }}</span>
                </div>
              </div>
            </div>

            <div class="item-price">
              <div class="original-price" v-if="item.item.price > item.item.discounted_price">
                ¥{{ item.item.price }}
              </div>
              <div class="current-price">
                ¥{{ item.item.discounted_price }}
              </div>
            </div>

            <div class="item-quantity">
              <el-input-number 
                :min="1" 
                :max="item.item.stock ?? 0" 
                v-model="item.count" 
                @change="updateShopCar"
                size="small"
                controls-position="right"
              />
              <div class="stock-tips">库存{{ item.item.stock }}件</div>
            </div>

            <div class="item-total">
              <div class="total-price">
                ¥{{ (item.item.discounted_price * item.count).toFixed(2) }}
              </div>
            </div>

            <div class="item-action">
              <el-button 
                type="danger" 
                text 
                @click="deleteGoods(index)"
                class="delete-btn"
              >
                <i class="el-icon-delete"></i>
                删除
              </el-button>
            </div>
          </div>
        </div>

        <!-- 购物车底部 -->
        <div class="cart-footer">
          <div class="footer-left">
            <div class="total-count">
              共 <span class="count-number">{{ totalItems }}</span> 件商品
            </div>
            <div class="total-amount-mobile">
              合计：<span class="amount-number">¥{{ totalAmount }}</span>
            </div>
          </div>
          <div class="footer-right">
            <div class="total-amount">
              合计：<span class="amount-number">¥{{ totalAmount }}</span>
            </div>
            <el-button 
              type="primary" 
              size="large" 
              class="checkout-btn"
              @click="goToStep2"
            >
              立即下单
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 步骤2: 确认订单 -->
    <div v-if="currentStep === 2" class="step-content">
      <div class="confirm-order">
        <div class="confirm-header">
          <h2>确认订单信息</h2>
          <el-button type="text" @click="backToStep1">
            <i class="el-icon-arrow-left"></i>
            返回修改
          </el-button>
        </div>

        <!-- 收货地址 -->
        <div class="address-section">
          <div class="section-title">收货地址</div>
          <el-input 
            v-model="address" 
            placeholder="请输入详细收货地址"
            size="large"
            class="address-input"
          />
        </div>

        <!-- 商品清单 -->
        <div class="order-goods">
          <div class="section-title">商品清单</div>
          <div class="goods-list">
            <div v-for="(item, index) in tableData" :key="index" class="order-item">
              <div class="order-item-image">
                <el-image :src="item.item.image" fit="cover" />
              </div>
              <div class="order-item-info">
                <div class="order-item-name">{{ item.item.name }}</div>
                <div class="order-item-desc">{{ item.item.description }}</div>
              </div>
              <div class="order-item-price">¥{{ item.item.discounted_price }}</div>
              <div class="order-item-quantity">×{{ item.count }}</div>
              <div class="order-item-total">¥{{ (item.item.discounted_price * item.count).toFixed(2) }}</div>
            </div>
          </div>
        </div>

        <!-- 订单汇总 -->
        <div class="order-summary">
          <div class="summary-item">
            <span>商品数量：</span>
            <span>{{ totalItems }}件</span>
          </div>
          <div class="summary-item">
            <span>商品总额：</span>
            <span>¥{{ totalAmount }}</span>
          </div>
          <div class="summary-item total">
            <span>应付总额：</span>
            <span class="total-amount">¥{{ totalAmount }}</span>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="confirm-actions">
          <el-button size="large" @click="backToStep1">上一步</el-button>
          <el-button 
            type="primary" 
            size="large" 
            @click="confirmOrder"
            :loading="confirmLoading"
            class="confirm-btn"
          >
            {{ confirmLoading ? '下单中...' : '确认下单' }}
          </el-button>
        </div>
      </div>
    </div>

    <!-- 步骤3: 支付完成 -->
    <div v-if="currentStep === 3" class="step-content">
      <div class="success-page">
        <div class="success-icon">
          <i class="el-icon-success"></i>
        </div>
        <div class="success-title">下单成功！</div>
        <div class="success-desc">订单已创建，等待商家发货</div>
        <div class="success-actions">
          <el-button type="primary" @click="goToOrders">查看订单</el-button>
          <el-button @click="continueShopping">继续购物</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, computed } from 'vue';
import { RequestPath, startRequest } from '../base/RequestWork';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import Account from '../base/AccountStore';

// 添加类型定义
interface Category {
  name: string;
}

interface CartItem {
  id: number;
  name: string;
  brand: string;
  description: string;
  price: number;
  discounted_price: number;
  stock: number;
  image: string;
  category: Category;
}

interface CartGoods {
  item: CartItem;
  count: number;
}

// 原有逻辑完全保持不变
let tableData = ref<CartGoods[]>([])
let router = useRouter()
let address = ref("")
let account = Account()

// 新增状态管理
let currentStep = ref(1) // 1:购物车, 2:确认订单, 3:支付完成
let confirmLoading = ref(false)

// 计算属性 - 计算总金额
const totalAmount = computed(() => {
  return tableData.value.reduce((total, item) => {
    return total + (item.item.discounted_price * item.count)
  }, 0).toFixed(2)
})

// 计算属性 - 计算总商品数量（修复问题）
const totalItems = computed(() => {
  return tableData.value.reduce((total, item) => {
    return total + item.count
  }, 0)
})

// 步骤导航方法
function goToStep2() {
  if (tableData.value.length === 0) {
    ElMessage.warning('购物车为空，无法下单')
    return
  }
  currentStep.value = 2
}

function backToStep1() {
  currentStep.value = 1
}

function goToStep3() {
  currentStep.value = 3
}

onMounted(()=>{
    if (!account.isLogin) {
        ElMessage.warning('请先登录后再查看购物车')
        router.push({
            name: 'login'
        })
        return
    }
    
    startRequest(RequestPath.shopGet, 'get', {}).then((response)=>{
        tableData.value = response.data.data.goods;
    }).catch((error)=>{
        ElMessage.error({
            message: error.response.data.error
        })
    }) 
})

function updateShopCar() {
    startRequest(RequestPath.shopUpdate, 'post', {
        goods: tableData.value
    })
}

function deleteGoods(idx: number) {
    tableData.value.splice(idx, 1)
    updateShopCar()
}

function confirmOrder() {
    if (address.value.length == 0) {
        ElMessage.error({
            message: "请填写收货地址"
        })
        return
    }
    
    confirmLoading.value = true
    startRequest(RequestPath.orderCreate, 'post', {address: address.value}).then((response)=>{
        if (response.data.msg != 'ok') {
            ElMessage.error({
                message: response.data.error
            })
            confirmLoading.value = false
        } else {
            // 创建订单成功
            confirmLoading.value = false
            goToStep3() // 进入支付完成步骤
            
            // 清空购物车
            tableData.value = []
            updateShopCar()
        }
    }).catch((error)=>{
        ElMessage.error({
            message: error.response.data.error
        })
        confirmLoading.value = false
    })
}

function goToOrders() {
  router.push({
    name: 'order'
  })
}

function continueShopping() {
  router.push({
    name: 'home'
  })
}
</script>

<style scoped>
.cart-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background: #fff;
  min-height: 100vh;
}

/* 页面头部 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 30px;
}

.header-title {
  display: flex;
  align-items: center;
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.header-title i {
  margin-right: 10px;
  color: #409eff;
  font-size: 28px;
}

.header-steps {
  display: flex;
  align-items: center;
}

.step-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.step-number {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #ccc;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  margin-bottom: 5px;
  transition: all 0.3s;
}

.step-item.active .step-number {
  background: #409eff;
  transform: scale(1.1);
}

.step-text {
  font-size: 12px;
  color: #666;
  transition: all 0.3s;
}

.step-item.active .step-text {
  color: #409eff;
  font-weight: 500;
}

.step-line {
  width: 80px;
  height: 2px;
  background: #f0f0f0;
  margin: 0 20px;
  transition: all 0.3s;
}

.step-line.active {
  background: #409eff;
}

/* 步骤内容 */
.step-content {
  min-height: 400px;
}

/* 确认订单页面 */
.confirm-order {
  max-width: 800px;
  margin: 0 auto;
}

.confirm-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.confirm-header h2 {
  margin: 0;
  color: #333;
}

.address-section {
  margin-bottom: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.section-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 15px;
}

.address-input {
  width: 100%;
}

.order-goods {
  margin-bottom: 30px;
}

.goods-list {
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  overflow: hidden;
}

.order-item {
  display: flex;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  gap: 15px;
}

.order-item:last-child {
  border-bottom: none;
}

.order-item-image {
  width: 60px;
  height: 60px;
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
}

.order-item-info {
  flex: 1;
}

.order-item-name {
  font-weight: 500;
  color: #333;
  margin-bottom: 5px;
}

.order-item-desc {
  font-size: 12px;
  color: #999;
}

.order-item-price,
.order-item-quantity,
.order-item-total {
  color: #333;
  font-weight: 500;
}

.order-item-total {
  color: #ff4444;
}

.order-summary {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 30px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  font-size: 14px;
  color: #666;
}

.summary-item.total {
  border-top: 1px solid #e0e0e0;
  padding-top: 10px;
  margin-top: 10px;
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.total-amount {
  color: #ff4444;
  font-weight: 700;
}

.confirm-actions {
  display: flex;
  justify-content: center;
  gap: 20px;
  padding: 20px 0;
}

.confirm-btn {
  padding: 0 40px;
}

/* 成功页面 */
.success-page {
  text-align: center;
  padding: 60px 20px;
}

.success-icon {
  font-size: 80px;
  color: #67C23A;
  margin-bottom: 20px;
}

.success-title {
  font-size: 24px;
  font-weight: 500;
  color: #333;
  margin-bottom: 10px;
}

.success-desc {
  color: #666;
  margin-bottom: 30px;
}

.success-actions {
  display: flex;
  justify-content: center;
  gap: 20px;
}
.cart-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background: #fff;
  min-height: 100vh;
}

/* 页面头部 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 30px;
}

.header-title {
  display: flex;
  align-items: center;
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.header-title i {
  margin-right: 10px;
  color: #409eff;
  font-size: 28px;
}

.header-steps {
  display: flex;
  align-items: center;
}

.step-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.step-number {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #ccc;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  margin-bottom: 5px;
}

.step-item.active .step-number {
  background: #409eff;
}

.step-text {
  font-size: 12px;
  color: #666;
}

.step-line {
  width: 80px;
  height: 2px;
  background: #f0f0f0;
  margin: 0 20px;
}

/* 空购物车 */
.empty-cart {
  text-align: center;
  padding: 80px 0;
}

.empty-icon {
  font-size: 80px;
  color: #ccc;
  margin-bottom: 20px;
}

.empty-text {
  font-size: 18px;
  color: #666;
  margin-bottom: 10px;
}

.empty-desc {
  font-size: 14px;
  color: #999;
  margin-bottom: 30px;
}

/* 购物车头部 */
.cart-header {
  display: grid;
  grid-template-columns: 3fr 1fr 1fr 1fr 80px;
  padding: 15px 20px;
  background: #f8f9fa;
  border-radius: 8px;
  margin-bottom: 15px;
  font-weight: 500;
  color: #333;
}

/* 购物车商品项 */
.cart-items {
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  overflow: hidden;
}

.cart-item {
  display: grid;
  grid-template-columns: 3fr 1fr 1fr 1fr 80px;
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  align-items: center;
}

.cart-item:last-child {
  border-bottom: none;
}

/* 商品信息 */
.item-product {
  display: flex;
  align-items: flex-start;
  gap: 15px;
}

.product-image {
  width: 80px;
  height: 80px;
  border: 1px solid #f0f0f0;
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
}

.goods-image {
  width: 100%;
  height: 100%;
}

.image-error {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  color: #ccc;
}

.product-info {
  flex: 1;
}

.product-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 5px;
  line-height: 1.4;
}

.product-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2; /* 添加标准属性 */
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.product-meta {
  display: flex;
  gap: 15px;
}

.product-brand, .product-category {
  font-size: 12px;
  color: #999;
  padding: 2px 8px;
  background: #f5f5f5;
  border-radius: 4px;
}

/* 价格 */
.item-price {
  text-align: center;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
  margin-bottom: 5px;
}

.current-price {
  font-size: 16px;
  color: #ff4444;
  font-weight: 500;
}

/* 数量 */
.item-quantity {
  text-align: center;
}

.stock-tips {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

/* 小计 */
.item-total {
  text-align: center;
}

.total-price {
  font-size: 16px;
  color: #ff4444;
  font-weight: 500;
}

/* 操作 */
.item-action {
  text-align: center;
}

.delete-btn {
  color: #ff4444;
  border: none;
}

.delete-btn:hover {
  background: #fff2f0;
}

/* 购物车底部 */
.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  margin-top: 30px;
}

.footer-left {
  display: flex;
  align-items: center;
}

.total-count {
  font-size: 14px;
  color: #666;
}

.count-number {
  color: #ff4444;
  font-weight: 500;
}

.footer-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.total-amount {
  font-size: 16px;
  color: #333;
}

.amount-number {
  font-size: 24px;
  color: #ff4444;
  font-weight: 700;
}

.checkout-btn {
  padding: 12px 40px;
  font-size: 16px;
  background: #ff4444;
  border-color: #ff4444;
}

.checkout-btn:hover {
  background: #ff6666;
  border-color: #ff6666;
}

/* 对话框样式 */
.order-dialog {
  border-radius: 12px;
}

.dialog-content {
  padding: 10px 0;
}

.section-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.address-section {
  margin-bottom: 30px;
}

.order-summary {
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  padding: 20px;
  background: #f8f9fa;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  font-size: 14px;
  color: #666;
}

.summary-item.total {
  border-top: 1px solid #e0e0e0;
  padding-top: 10px;
  margin-top: 10px;
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.total-amount {
  color: #ff4444;
  font-weight: 700;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
}

.confirm-btn {
  background: #ff4444;
  border-color: #ff4444;
  padding: 0 30px;
}

.confirm-btn:hover {
  background: #ff6666;
  border-color: #ff6666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .cart-container {
    padding: 10px;
  }
  
  .page-header {
    flex-direction: column;
    gap: 20px;
  }
  
  .cart-header, .cart-item {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .cart-footer {
    flex-direction: column;
    gap: 20px;
    text-align: center;
  }
  
  .footer-right {
    flex-direction: column;
    gap: 15px;
  }
}.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  margin-top: 30px;
}

.footer-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.total-count {
  font-size: 14px;
  color: #666;
}

.count-number {
  color: #ff4444;
  font-weight: 500;
}

.total-amount-mobile {
  display: none;
  font-size: 16px;
  color: #333;
}

.footer-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.total-amount {
  font-size: 16px;
  color: #333;
}

.amount-number {
  font-size: 24px;
  color: #ff4444;
  font-weight: 700;
}

.checkout-btn {
  padding: 12px 40px;
  font-size: 16px;
  background: #ff4444;
  border-color: #ff4444;
}

.checkout-btn:hover {
  background: #ff6666;
  border-color: #ff6666;
}

/* 步骤条激活状态 */
.step-item.active .step-number {
  background: #409eff;
}

.step-item.active .step-text {
  color: #409eff;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .cart-container {
    padding: 10px;
  }
  
  .page-header {
    flex-direction: column;
    gap: 20px;
  }
  
  .cart-header, .cart-item {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .cart-footer {
    flex-direction: column;
    gap: 20px;
    text-align: center;
  }
  
  .footer-right {
    flex-direction: column;
    gap: 15px;
  }
  
  .total-amount {
    display: none;
  }
  
  .total-amount-mobile {
    display: block;
  }
}
</style>